package com.pantuo.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.pantuo.sys.pojo.Item;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 炳秦
 * @Date: 2021/10/12/11:46
 * @Description:
 */
@Service
public class EsService {

    @Autowired
    private RestHighLevelClient client;

    public void testInsert2(){
        //112.6.162.57:9023
        String url = "jdbc:mysql://112.6.162.57:9023/security?serverTimezone=GMT%2B8&characterEncoding=utf8";
        String user = "root";
        String password = "pantuo1709";
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int index = 1;
        int length = 10000;
        int currentPage = 1;
        try {
            while (index <= 100000){
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection(url, user, password);
                String sql = "select * from tb_item where id >= "+index+" limit "+ length+"";
                //String sql = "select * from tb_item where id <= 500000";
                statement = conn.prepareStatement(sql);
                //关键：做一下设置会采用流数据接收方式，每次从服务器接收部分数据，直到所有数据处理完毕，不会发生JVM OOM
                statement.setFetchSize(Integer.MIN_VALUE);
                resultSet = statement.executeQuery();
                List<Item> itemList = new ArrayList<>();
                BulkRequest bulkRequest = new BulkRequest();
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String title = resultSet.getString("title");
                    String price = resultSet.getString("price");
                    int num = resultSet.getInt("num");
                    long categoryid = resultSet.getLong("categoryid");
                    String status = resultSet.getString("status");
                    String sellerid = resultSet.getString("sellerid");
                    Date createtime = resultSet.getDate("createtime");
                    Date updatetime = resultSet.getDate("updatetime");
                    itemList.add(new Item(id, title, price, num, categoryid, status, sellerid, createtime, updatetime));
                }
                for (int i = 0; i < itemList.size(); i++) {
                    bulkRequest.add(new IndexRequest("oa_data_01")
                            .source(JSON.toJSONString(itemList.get(i)), XContentType.JSON));
                }
                TimeValue took = client.bulk(bulkRequest, RequestOptions.DEFAULT).getTook();
                index = (currentPage * length) + 1;
                currentPage = currentPage + 1;
                System.out.println("花费时间：" + took);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

}
